import { evalEl, getProcessElTrueData, sortKey, evalElKeyInVal } from './auth'
import { hasPermissionInDB } from './hypermission'
import { hasModalFn } from './hyfn'

/**
 * 当前应用按钮
 * @param thisObj 对象
 * @param btnList 按钮列表
 * @returns {*[]|*}
 */
export function getAppBtnList(thisObj, btnList) {
  const newBtnList = []
  let listKey = []
  if (btnList === undefined || btnList.length === 0) {
    return newBtnList
  }
  if (thisObj.initUiVal.sessionData !== undefined && thisObj.initUiVal.sessionData.sessionBtn !== undefined) {
    if (thisObj.initUiVal.sessionData.sessionBtn.listKey.length > 0) {
      listKey = thisObj.initUiVal.sessionData.sessionBtn.listKey
    }
  } else if (thisObj.initUiVal.appData !== undefined && thisObj.initUiVal.appData.appBtn !== undefined) {
    // 存在定制应用,以定制应用配置为主
    if (thisObj.initUiVal.appData.appBtn.listKey.length > 0) {
      listKey = thisObj.initUiVal.appData.appBtn.listKey
    }
  }
  if (listKey.length === 0) {
    return btnList
  }
  const btnListLen = btnList.length
  const listKeyLen = listKey.length
  for (let i = 0; i < listKeyLen; i++) {
    const key = listKey[i]
    for (let j = 0; j < btnListLen; j++) {
      const tmpKey = btnList[j]
      if (key === tmpKey.key) {
        newBtnList.push(tmpKey)
        break
      }
    }
  }
  return newBtnList
}

export function initToolbar(thisObj) {
  // 一开始初始化按钮
  // thisObj.initBtnData = Object.assign({}, thisObj.initUiVal.toolbar.btnData)
  thisObj.initBtnEvent = Object.assign({}, thisObj.initUiVal.toolbar.btnEvent)
  const btnList = thisObj.initUiVal.btnGroup.btnList
  const toolbarArr = getInitBtnList(thisObj, btnList)
  thisObj.tableToolbarList = Object.assign([], toolbarArr)

  if (thisObj.initUiVal.toolbar !== null) {
    thisObj.initLoadObj.loadBtnId = thisObj.initUiVal.toolbar.loadBtnId
  }
}

export function initFooterToolbarList(thisObj) {
  if (thisObj.initUiVal.btnGroup !== undefined) {
    const footer = thisObj.initUiVal.btnGroup.footer
    const footerArr = getInitBtnList(thisObj, footer)
    thisObj.footerToolbarList = Object.assign([], footerArr)
  }
  initUiLabel(thisObj)
}

export function initUiLabel(thisObj) {
  const srcLabel = thisObj.initSrcObj.initSrcLabel
  if (srcLabel === 'detail') {
    thisObj.disableSubmit = true
  } else {
    thisObj.disableSubmit = false
  }
}

export function initToolbarMenu(thisObj) {
  // 一开始初始化按钮下拉框
  const btnList = thisObj.initUiVal.btnGroup.menuList
  const menuArr = getInitBtnList(thisObj, btnList)
  thisObj.tableToolbarMenu = Object.assign([], menuArr)
}

export function hiddenActiveBtnKey(thisObj, S_RULE_TPL_KEY, globalData) {
  thisObj.tableToolbarList = Object.assign([], removeHiddenActiveBtnKey(thisObj, S_RULE_TPL_KEY, thisObj.tableToolbarList))
  thisObj.tableToolbarMenu = Object.assign([], removeHiddenActiveBtnKey(thisObj, S_RULE_TPL_KEY, thisObj.tableToolbarMenu))
}

export function showActiveBtnKey(thisObj, S_RULE_TPL_KEY, globalData) {
  thisObj.tableToolbarList = Object.assign([], appendActiveBtnKey(thisObj, S_RULE_TPL_KEY, thisObj.tableToolbarList, globalData, 1))
  thisObj.tableToolbarMenu = Object.assign([], appendActiveBtnKey(thisObj, S_RULE_TPL_KEY, thisObj.tableToolbarMenu, globalData, 2))
}

export function appendActiveBtnKey(thisObj, btnKey, activeBtnList, record, toolbarObj) {
  const btnList = Object.assign([], activeBtnList)
  const initBtnData = thisObj.initUiVal.toolbar.btnData
  const tmpBtn = hasVisualKeyBtn(thisObj, initBtnData, btnKey, record)
  if (toolbarObj === 1) {
    if (!(tmpBtn.btnSrc === 1 || tmpBtn.btnSrc === 3)) {
      return btnList
    }
  }
  if (tmpBtn !== undefined && tmpBtn !== null && tmpBtn.show === true) {
    let hasTmpBtn = false
    for (const i in activeBtnList) {
      const btn = activeBtnList[i]
      if (btn.key === btnKey) {
        hasTmpBtn = true
        break
      }
    }
    if (!hasTmpBtn) {
      btnList.push(tmpBtn)
    }
  }
  return sortKey(btnList, 'sort')
}

export function removeHiddenActiveBtnKey(thisObj, S_RULE_TPL_KEY, activeBtnList) {
  const btnList = []
  for (const i in activeBtnList) {
    const btn = activeBtnList[i]
    if (btn.key !== S_RULE_TPL_KEY) {
      btnList.push(btn)
    }
  }
  return btnList
}

export function initColumnListBtn(thisObj) {
  if (thisObj.initUiVal.columnsList !== undefined) {
    const changeColumnBtnAction = initColumnListBtnData(thisObj, thisObj.listColActiveList)
    thisObj.columnBtnAction = Object.assign({}, changeColumnBtnAction)
  }
}

/**
 * 组装按钮数据
 * @param thisObj
 * @param columnsList
 */
export function initColumnListBtnData(thisObj, columnsList) {
  const columnBtn = Object.assign({})
  if (columnsList === null || undefined === columnsList) {
    return columnBtn
  }
  const columnLen = columnsList.length
  for (let i = 0; i < columnLen; i++) {
    const columnData = columnsList[i]
    const dataIndex = columnData.dataIndex
    const columnTmpData = initColumnListBtnKey(thisObj, columnData)
    if (columnTmpData !== null) {
      columnBtn[dataIndex] = columnTmpData
    }
  }
  return columnBtn
}

export function initColumnListBtnKey(thisObj, columnData) {
  const columnBtnData = Object.assign({})
  let btnArr = []
  const columnsBtnList = []
  const columnsMenuList = []
  if (undefined !== columnData.btnList) {
    const btnList = getAppBtnList(thisObj, columnData.btnList)
    btnArr = getInitBtnList(thisObj, btnList)
  }
  if (btnArr.length === 0) {
    return null
  }
  for (const i in btnArr) {
    const tmpBtn = btnArr[i]
    if (tmpBtn.btnSrc === 1 || tmpBtn.btnSrc === 3) {
      // 按钮条或btn类
      columnsBtnList.push(tmpBtn)
    } else {
      columnsMenuList.push(tmpBtn)
    }
  }
  columnBtnData.columnsBtnList = columnsBtnList
  columnBtnData.columnsMenuList = columnsMenuList
  return columnBtnData
}

export function getAppBtnData(thisObj) {
  let appBtnData = {}
  if (thisObj.initUiVal.sessionData !== undefined && thisObj.initUiVal.sessionData.sessionBtn !== undefined) {
    // 存在定制应用,以定制应用配置为主
    appBtnData = thisObj.initUiVal.sessionData.sessionBtn.listData
  } else if (thisObj.initUiVal.appData !== undefined) {
    // 存在定制应用,以定制应用配置为主
    appBtnData = thisObj.initUiVal.appData.appBtn.listData
  }
  return appBtnData
}

export function getInitBtnList(thisObj, btnList) {
  const appBtnData = getAppBtnData(thisObj)
  const initBtnData = thisObj.initUiVal.toolbar.btnData
  const toolbarArr = []
  for (const i in btnList) {
    const initBtn = btnList[i]
    let tmpBtn = initBtnData[initBtn.key]
    if (tmpBtn !== undefined) {
      tmpBtn = Object.assign({}, tmpBtn)
      if (hasPermissionInDB(tmpBtn.authCode)) {
        if (appBtnData[tmpBtn.key] !== undefined) {
          tmpBtn.name = appBtnData[tmpBtn.key].name
        } else {
          if (initBtn.name !== undefined && initBtn !== null) {
            // 以关系表配置名称为主
            tmpBtn.name = initBtn.name
          }
        }
        tmpBtn.objBtn = initBtn
        toolbarArr.push(tmpBtn)
      }
    }
  }
  return toolbarArr
}

export function changeBtnList(thisObj, btnList, globalData) {
  const appBtnData = getAppBtnData(thisObj)
  const initBtnData = thisObj.initUiVal.toolbar.btnData
  const toolbarArr = []
  for (const i in btnList) {
    const initBtn = btnList[i]
    const btnKey = initBtn.key
    if (btnKey === undefined) {
      console.log('异常数据#', initBtn, '#全部按钮数据#', btnList)
      continue
    }
    let tmpBtn = hasVisualKeyBtn(thisObj, initBtnData, btnKey, globalData)
    if (tmpBtn !== undefined && tmpBtn !== null && tmpBtn.show === true) {
      tmpBtn = Object.assign({}, tmpBtn)
      if (appBtnData[tmpBtn.key] !== undefined) {
        tmpBtn.name = appBtnData[tmpBtn.key].name
      } else {
        if (initBtn.name !== undefined && initBtn !== null) {
          // 以关系表配置名称为主
          tmpBtn.name = initBtn.name
        }
      }
      tmpBtn.objBtn = initBtn
      toolbarArr.push(tmpBtn)
    }
  }
  return toolbarArr
}

export function hasVisualKeyBtn(thisObj, initBtnData, btnKey, globalData) {
  const btnData = initBtnData[btnKey]
  if (btnData === undefined) {
    return null
  }
  if (btnData.visualBtn === 2) {
    return null
  }
  const tmpBtnData = Object.assign({}, btnData)
  if (hasPermissionInDB(tmpBtnData.authCode)) {
    const hasBtnFn = hasBtnModalFn(thisObj, globalData, btnKey)
    if (hasBtnFn[btnKey]) {
      return changeToolbarKey(thisObj, globalData, tmpBtnData)
    }
  }
  return null
}

export function changeToolbar(thisObj, triggerObj, globalData) {
  // 一开始每次加载按钮
  const btnList = getAppBtnList(thisObj, thisObj.initUiVal.btnGroup.btnList)
  const toolbarArr = changeBtnList(thisObj, btnList, globalData)
  const cc = Object.assign([], toolbarArr)
  thisObj.tableToolbarList = Object.assign([], cc)
}

export function changeToolbarMenu(thisObj, triggerObj, globalData) {
  // 一开始每次加载按钮下拉
  const menuList = getAppBtnList(thisObj, thisObj.initUiVal.btnGroup.menuList)
  const menuArr = changeBtnList(thisObj, menuList, globalData)
  const cc = Object.assign([], menuArr)
  thisObj.tableToolbarMenu = Object.assign([], cc)
}

export function changeFooterToolbarList(thisObj, triggerObj, globalData) {
  if (thisObj.initUiVal.btnGroup !== undefined) {
    const footerList = getAppBtnList(thisObj, thisObj.initUiVal.btnGroup.footer)
    const footerArr = changeBtnList(thisObj, footerList, globalData)
    const cc = Object.assign([], footerArr)
    thisObj.footerToolbarList = Object.assign([], cc)
    //
    // const footer = thisObj.initUiVal.btnGroup.footer
    // const footerArr = getInitBtnList(thisObj, footer)
    // thisObj.footerToolbarList = Object.assign([], footerArr)
  }
}

export function changeFormBtn(thisObj, formItem) {
  let refOpenList = []
  let refExport = []
  let refImport = []
  if (thisObj.initUiVal.toolbar === undefined || thisObj.initUiVal.toolbar.btnData === undefined) {
    return
  }
  const initBtnData = thisObj.initUiVal.toolbar.btnData
  for (const i in formItem.formBtn) {
    const tmpBtn = formItem.formBtn[i]
    const keyBtn = initBtnData[tmpBtn.key]
    if (keyBtn === undefined) {
      return
    }
    if (formItem.compTag === 'inputSelect' || formItem.compTag === 'inputButton') {
      refOpenList = Object.assign([], refOpenList, keyBtn.refOpenList)
    } else if (formItem.compTag === 'downFile') {
      refExport = Object.assign([], refExport, keyBtn.refExport)
    } else if (formItem.compTag === 'uploadFile' || formItem.compTag === 'uploadImg') {
      refImport = Object.assign([], refOpenList, keyBtn.refImport)
    }
  }
  if (formItem.compTag === 'inputSelect' || formItem.compTag === 'inputButton') {
    formItem.refOpenList = refOpenList
  } else if (formItem.compTag === 'downFile') {
    formItem.refExport = refExport
  } else if (formItem.compTag === 'uploadFile' || formItem.compTag === 'uploadImg') {
    formItem.refImport = refImport
  }
}

export function hasBtnModalFn(thisObj, globalData, btnKey) {
  return hasModalFn(thisObj, globalData, 'btn', btnKey)
  // const srcLabel = thisObj.initSrcObj.initSrcLabel
  // const tmpObj = {}
  // tmpObj[btnKey] = true
  // if (srcLabel === undefined) {
  //   return tmpObj
  // }
  // if (thisObj.initUiVal.modalFn !== undefined) {
  //   if (thisObj.initUiVal.modalFn[srcLabel]) {
  //     const btnFnObj = thisObj.initUiVal.modalFn[srcLabel].btn
  //     if (btnFnObj !== undefined) {
  //       const dataList = btnFnObj[btnKey]
  //       if (dataList === undefined) {
  //         // 没配置 读写
  //         tmpObj[btnKey] = false
  //       } else {
  //         const elObj = getProcessElTrueData(thisObj, btnKey, dataList, globalData, true)
  //         if (elObj[btnKey]) {
  //           // 配置的时只读属性
  //           tmpObj[btnKey] = true
  //         } else {
  //           tmpObj[btnKey] = false
  //         }
  //       }
  //     }
  //   }
  // }
  // return tmpObj
}
export function changeToolbarMenuData(thisObj, globalData, toolbarList) {
  // 一开始每次加载按钮
  const initBtnData = thisObj.initUiVal.toolbar.btnData
  const changeToolbarList = Object.assign([])
  if (toolbarList !== undefined && toolbarList.length > 0) {
    for (const i in toolbarList) {
      const btnData = toolbarList[i]
      const btnKey = btnData.key

      const tmpBtn = hasVisualKeyBtn(thisObj, initBtnData, btnKey, globalData)
      if (tmpBtn !== undefined && tmpBtn !== null && tmpBtn.show === true) {
        changeToolbarList.push(tmpBtn)
      }
    }
  }
  return changeToolbarList
}

export function changeToolbarKey(thisObj, globalData, btnData) {
  if (btnData === null || undefined === btnData || btnData === '') {
    return null
  }
  const elLocalKey = evalElLocalTmpDataRow(btnData, globalData)
  if (!elLocalKey) {
    return null
  }
  const elKey = evalElKeyInVal(btnData.elKey, btnData.elKeyInVal, globalData.globalParam)
  if (!elKey) {
    return null
  }
  const objEl = btnData.objEl
  if (undefined === objEl) {
    btnData.show = true
    return btnData
  }
  if (evalEl(objEl, globalData.globalParam)) {
    // const elKey = evalElKeyInVal(btnData.elKey, btnData.elKeyInVal, globalData.globalParam)
    // if (elKey) {
    btnData.show = true
    return btnData
    // }
  }
  return null
}
export function evalElLocalTmpDataRow(btnData, globalData) {
  if (globalData.clickRow !== undefined && globalData.clickRow.hyLocalTmpData === 'hyLocalTmpData') {
    if (btnData.tmpDataShow === 2) {
      return true
    } else {
      return false
    }
  }
  return true
}

export function initRowBtn(thisObj, globalData, rowIndex) {
  if (thisObj.initUiVal.columnsList !== undefined) {
    const btnKey = thisObj.initUiVal.columnsList.columnBtnKey
    for (const col in btnKey) {
      const btnTmpObj = {}
      const menuTmpObj = {}
      const dataIndex = btnKey[col]
      let btnAction = thisObj.columnBtnAction[dataIndex]
      if (btnAction === undefined) {
        if (dataIndex !== 'action') {
          // continue
          const tmpObj = getRowDefaultBtn()
          btnTmpObj[rowIndex] = tmpObj
          menuTmpObj[rowIndex] = []
          btnAction = tmpObj
        } else {
          btnTmpObj[rowIndex] = []
          menuTmpObj[rowIndex] = []
        }
      } else {
        if (undefined !== btnAction.columnsBtnList) {
          let tmpObj = changeToolbarMenuData(thisObj, globalData, btnAction.columnsBtnList)
          if (dataIndex !== 'action') {
            if (tmpObj.length === 0) {
              // 满满足条件按钮,应该直接呈现文本
              tmpObj = getRowDefaultBtn()
            } else {
              tmpObj[0].name = ''
            }
          }
          btnTmpObj[rowIndex] = tmpObj
        } else {
          btnTmpObj[rowIndex] = []
        }
      }
      if (btnAction === undefined) {
        if (dataIndex === 'action') {
          menuTmpObj[rowIndex] = []
          btnTmpObj[rowIndex] = []
        }
      } else {
        if (undefined !== btnAction.columnsMenuList) {
          const tmpObj = changeToolbarMenuData(thisObj, globalData, btnAction.columnsMenuList)
          menuTmpObj[rowIndex] = tmpObj
        } else {
          menuTmpObj[rowIndex] = []
        }
      }
      thisObj.customRowIdxBtnObj[dataIndex] = btnTmpObj
      thisObj.customRowIdxMenuObj[dataIndex] = menuTmpObj
    }
  }
}

export function getRowDefaultBtn() {
  const rowBtn = {}
  rowBtn.compTag = 'text'
  rowBtn.eventName = 'click'
  const rowBtnArr = []
  rowBtnArr.push(rowBtn)
  return rowBtnArr
}

/**
 * 组装按钮数据
 * @param thisObj
 * @param columnsList
 */
export function changeColumnListBtnData(thisObj, columnsList) {
  const columnBtn = Object.assign({})
  if (columnsList === null || undefined === columnsList) {
    return columnBtn
  }
  const columnLen = columnsList.length
  for (let i = 0; i < columnLen; i++) {
    const columnData = columnsList[i]
    const dataIndex = columnData.dataIndex
    const columnTmpData = changeColumnListBtnKey(columnData)
    if (columnTmpData !== null) {
      columnBtn[dataIndex] = columnTmpData
    }
  }
  return columnBtn
}

export function changeColumnListBtnKey(columnData) {
  const columnBtnData = Object.assign({})
  let hasBtn = false
  if (undefined !== columnData.columnsBtnList) {
    hasBtn = true
    columnBtnData.columnsBtnList = columnData.columnsBtnList
  }
  if (undefined !== columnData.columnsMenuList) {
    hasBtn = true
    columnBtnData.columnsMenuList = columnData.columnsMenuList
  }
  if (hasBtn) {
    return columnBtnData
  } else {
    return null
  }
}

export function changeTreeColumnMenu(thisObj, triggerObj, globalData) {
  // console.log('00000000globalData00000000', globalData)
  // 一开始每次加载按钮下拉
  let columnMenuList = []
  if (thisObj.columnBtnAction.action !== undefined) {
    let columnBtnTmpList = []
    let columnMenuTmpList = []
    if (thisObj.columnBtnAction.action.columnsBtnList !== undefined) {
      columnBtnTmpList = getAppBtnList(thisObj, thisObj.columnBtnAction.action.columnsBtnList)
    }
    if (thisObj.columnBtnAction.action.columnsMenuList !== undefined) {
      columnMenuTmpList = getAppBtnList(thisObj, thisObj.columnBtnAction.action.columnsMenuList)
    }
    columnMenuList = Object.assign([], columnBtnTmpList)
    for (const i in columnMenuTmpList) {
      columnMenuList.push(columnMenuTmpList[i])
    }
  } else {
    // columnMenuList = getAppBtnList(thisObj, thisObj.initUiVal.toolbar.columnsMenuList)
    columnMenuList = getAppBtnList(thisObj, thisObj.initUiVal.btnGroup.columnsMenuList)
  }
  const changeColumnMenuList = changeToolbarMenuData(thisObj, globalData, columnMenuList)
  // const cc = Object.assign([], thisObj.columnsMenuList, changeColumnMenuList)
  const cc = Object.assign([], changeColumnMenuList)
  thisObj.tableColumnMenu = Object.assign([], cc)
}
